/**
 * @filename:PickController Sep 23, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.vtuzx.core.VtuzxException;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.PrdPick;
import com.wonder.app.request.PrdPickRequest;
import com.wonder.app.service.PrdPickService;
import com.wonder.app.service.TrayService;
import com.wonder.framework.authorization.CheckAdminToken;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * <p>说明： 生产领料API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Sep 23, 2024
 *
 */
@Api(tags = "托盘")
@RestController
@RequestMapping("/app/Pick")
public class TrayController {

    @Autowired
    private TrayService trayService;

    @Autowired
    private CheckAdminToken checkAdminToken;

    @Autowired
    private SysAdminService adminService;

    private Map<Long,Long> lastRequestTimeMap = new ConcurrentHashMap<>();

    /**
     * 创建托盘信息
     * @param map
     * @return
     */
    @ApiOperation(value = "创建托盘信息")
    @ResponseBody
    @PostMapping(value = "/create_tray")
    public AjaxResult<Boolean> createPickBill(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        //从map中查找上次请求的时间戳
        Long lastRequestTime = lastRequestTimeMap.get(userId);
        //如果为空或者上次请求的时间戳与当前时间做差大于5s，则视为新请求，否则重复请求。
        if (lastRequestTime == null || System.currentTimeMillis() - lastRequestTime > 20000) {
            lastRequestTimeMap.put(userId, System.currentTimeMillis());
            // 处理请求
            Boolean res = trayService.createTray(map);
            if (res){
                return AjaxResult.success(res);
            } else {
                return AjaxResult.failed();
            }
        } else {
            // 重复请求
            throw new Exception("正在领料，请不要重复点击");
        }

    }


    /**
     * 创建托盘信息(手机端)
     * @param map
     * @return
     */
    @ApiOperation(value = "创建托盘信息(手机端)")
    @ResponseBody
    @PostMapping(value = "/create_tray_pda")
    public AjaxResult<?> createTrayPda(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();

        String res = trayService.createTrayPda(map,userName);

        return AjaxResult.success(res);

    }


    /**
     * 创建托盘信息(库管新增)
     * @param map
     * @return
     */
    @ApiOperation(value = "创建托盘信息(库管新增)")
    @ResponseBody
    @PostMapping(value = "/create_tray_pda_by_ku")
    public AjaxResult<?> createTrayPdaByKu(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();

        String res = trayService.createTrayPdaByKu(map,userName);

        return AjaxResult.success(res);

    }



    /**
     * 获取托盘信息
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取托盘信息")
    @ResponseBody
    @PostMapping(value = "/get_tray")
    public Map<String, Object> getTray (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
       return  trayService.getTray(map);
    }

    /**
     * 获取托盘信息(调拨单用)
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取托盘信息")
    @ResponseBody
    @PostMapping(value = "/get_tray_allocation")
    public Map<String, Object> getTrayAllocation (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        return  trayService.getTrayAllocation(map);
    }


    /**
     * 获取托盘信息
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取托盘信息(调拨申请扫码时用)")
    @ResponseBody
    @PostMapping(value = "/get_tray_transfer")
    public Map<String, Object> getTrayTransfer (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();

        return  trayService.getTrayTransfer(userId,map);
    }

    /**
     * 获取托盘信息(工序汇报时用)
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取托盘信息(工序汇报时用)")
    @ResponseBody
    @PostMapping(value = "/get_tray_plan")
    public Map<String, Object> getTrayPlan (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        return  trayService.getTrayPlan(map);
    }

    /**
     * 领料扣减
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "领料扣减")
    @ResponseBody
    @PostMapping(value = "/update_tray")
    public boolean updatetray (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        return  trayService.updateTray(map);
    }

    /**
     * 修改托盘数量
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "修改托盘数量")
    @ResponseBody
    @PostMapping(value = "/update_tray_count")
    public boolean updatetrayCount (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        return  trayService.updatetrayCount(map);
    }


    /**
     * 修改托盘数量(pda)
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "修改托盘数量")
    @ResponseBody
    @PostMapping(value = "/update_tray_count_by_pda")
    public boolean updatetrayCountByPda (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        return  trayService.updatetrayCountByPda(map);
    }

    @ApiOperation(value = "托盘列表(手机端)")
    @RequestMapping(value = "/get_tray_list", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getTrayList(@RequestBody Map<String,Object> map) throws Exception{

        Map<String,Object> data = trayService.getTrayList(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }
}